<?php

namespace mpend\filters;

use Yii;
use common\models\app\AppToken;

/**
 * HttpBearerAuth is an action filter that supports the authentication method based on HTTP Bearer token.
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class AppBearerAuth extends \common\filters\AppBearerAuth {

    /**
     * @var AppToken|null the HTTP authentication token
     */
    public $token = null;

    /**
     * @var AppToken|null the HTTP authentication token
     */
    public $identityClass = AppToken::class;

    /**
     * @inheritdoc
     */
    public function bindAuthenticate($identity) {
        $uid = $identity->user_id;
        $user = $uid ? $identity->user : null;
        if ($user) {
            Yii::$app->getUser()->login($user);
        }
        $this->uid = $uid;
        $this->token = $identity;
        $this->build = $identity->build;
    }

}
